package com.maxd.modules.selfshop.merchants.service;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.GoodsTypeResult;
import com.maxd.modules.selfshop.merchants.entity.SelfStoreType;
import com.maxd.modules.selfshop.merchants.respository.SelfStoreTypeJpaRepository;
import com.maxd.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SelfStoreTypeServiceImpl implements SelfStoreTypeService{
    @Autowired
    private SelfStoreTypeJpaRepository jpaRepository;

    @Override
    public Result findAll(Integer page, Integer size, Long storeId) {
        //按照时间排序
        Pageable pageable = PageRequest.of(page, size, Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime")));
        //构造自定义查询条件
        Specification<SelfStoreType> queryCondition = new Specification<SelfStoreType>() {
            @Override
            public Predicate toPredicate(Root<SelfStoreType> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (storeId != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("storeId"), storeId));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return ResultUtil.success(jpaRepository.findAll(queryCondition, pageable));
    }

    @Override
    public Result result(Long storeId) {
        List<SelfStoreType> type1 = jpaRepository.findByStoreId(storeId);
        List<GoodsTypeResult> resultList = new ArrayList<>();
        for (SelfStoreType g : type1) {
            GoodsTypeResult result = new GoodsTypeResult();
            result.setValue(g.getStoreTypeId());
            result.setLabel(g.getName());
            resultList.add(result);
        }
        return ResultUtil.success(resultList);
    }

    @Override
    public Result findByStore(Long storeId) {
        return ResultUtil.success(jpaRepository.findByStoreId(storeId));
    }


    @Override
    public Result saveBody(SelfStoreType entity) {
        entity.setCreateTime(DateUtil.createTime());
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result updateBody(SelfStoreType entity) {
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result findOne(Long id) {
        return ResultUtil.success(jpaRepository.findById(id).orElse(null));
    }



    @Override
    public Result delete(Long id) {
        jpaRepository.deleteById(id);
        return ResultUtil.success();
    }



}
